#include <avr/io.h>

#include "../../ACUAS/ACUAS.h"
#include "../../ACUAS/ACUASLCD-CSlab.h"		// The 2 x 16 character LCD is connected

void card_new(unsigned char no);
unsigned char shuffle (void);
unsigned char value_card(unsigned char n_no);
unsigned char total_player(void);

unsigned char last_name[52]={'A','2','3','4','5','6','7','8','9','0','J','Q','K','A','2','3','4','5','6','7','8','9','0','J','Q','K','A','2','3','4','5','6','7','8','9','0','J','Q','K','A','2','3','4','5','6','7','8','9','0','J','Q','K'};
unsigned char last_type[52]={'c','c','c','c','c','c','c','c','c','c','c','c','c','s','s','s','s','s','s','s','s','s','s','s','s','s','h','h','h','h','h','h','h','h','h','h','h','h','h','d','d','d','d','d','d','d','d','d','d','d','d','d'};

unsigned char game_player[5]={0};
unsigned char card=0;
unsigned char total=0;


int main(void)
{
	DDRA=0x00;
	PORTA=0xFF;	


	unsigned char new_game=0,other=0,finished=0;
	unsigned char follow=0,win=0,finish_game=0;
	unsigned char newcard=0;
	int init=0,j=0,i=0;

	
	ACTIVATE_LCD
	RANDOM_INIT


	 do{    //first shuffle
	 finished=shuffle();
	}while(finished!=1);


do{    //first shuffle
	 finished=shuffle();
	}while(finished!=1);


while(1)
{

	new_game=(PINA&0x01);
	other=(PINA&0x02);
	finish_game=(PINA&0x04);


		if(new_game==0 && init==0)
			{
	
	///////////////////////FIRSTCARD////////////////////////////////////////////////////////////
		LCD_XY(1,1)
				newcard=last_name[card];// card is the potition and newcard is the next card
				card_new(newcard); //funtion for printing the new card
				game_player[j]=newcard;	// cards that has been played			
				j++;				//count array game
				card++;              // main count 
		
    ///////////////////////SECONDCARD///////////////////////////////////////////////////////////			
		        newcard=last_name[card];
				card_new(newcard);
				game_player[j]=newcard;				
				j++;
				card++;
			
				init=1; // only for two times is iqual to 0 after is iqual to 1 
			}
	
	//////////////////////////NEXTCARDS/////////////////////////////////////////////////////
			if(other==0 && init==1 && follow==0)
			{

				newcard=last_name[card];//Current game is saved in this array
				LCD_XY(5,1)
				card_new(newcard);
		     
				game_player[j]=newcard;				
				j++;
				card++;
			}

/////////////////////////////////CONDITIONS//////////////////////////////////////////////////////////////
/// when the total > 21 return 1
/// when the total == 21 return 2
/// when the player do not want more cards return 3
/// when j == 5 this the number of cards played return 4

				total=total_player();//funtion check the addition

				LCD_XY(3,2)
				LCD_NUMBER(total)
				
				if(total>21)
				{
					follow=1;
				//	return 1;
					}
				else
				{
					follow=0;
					}

				if(total==21)
				{
						win=1;
					//return 2;		
						}

				if(finish_game==0)
					{
						follow=1;
					//	return 3;
						}

					if(j==4 && follow==0)
					{
					
					//	return 4;
						}

				if(new_game==0 && (follow==1 || win==1))  //////////////// FOR ANEW GAME
				{
				init=0;
				total=0;
				follow=0;
				win=0;
				j=0;

					for(i=0;i<5;i++)
					{
					game_player[i]=0;
					}
						LCD_XY(1,1)
						LCD_TEXT("        ");
						LCD_XY(3,2)
						LCD_TEXT("    ");
						}


/////////////////////////////////////////////////////////////////////////////////////////////////////////




	if(card>40)  // NEXT SHUFFLE
	{
		  do{
	 			finished=shuffle();
			}while(finished!=1);
			card=0;
	}



	}//end while
}


////////////////////////////////////PRINTCARD////////////////////////////////////////////

void card_new(unsigned char no)
{
	unsigned char game=0;
		
		game=no;

		if(game=='0')
		{
			LCD_TEXT("10");
			LCD_CHAR(last_type[card]);
		}
		else
		{
			LCD_CHAR(game);
			LCD_CHAR(last_type[card]);
		}
		
}




////////////////////////////////////////SHUFFLE///////////////////////////////////////////////////////

unsigned char shuffle (void)
{
unsigned char next_card=0,already=0;

int j=0,i=0;

unsigned char
 card_new_position[52]={0},new_name[52]={0},new_type[52]={0};

	next_card=RANDOM(1,52);
	next_card=next_card-1;

	while(j<52)
	{
		while(new_name[i]!=0)
		{
			if (next_card==card_new_position[i])
			already=1;
		i++;
		}//end while check
		i=0;
			if(already==0)
			{
				new_name[j]=last_name[next_card];
				new_type[j]=last_type[next_card];

				card_new_position[j]=next_card;
					next_card=RANDOM(1,52);
					next_card=next_card-1;
					j++;
					//return 0;
			}
			else
				{	
				next_card=RANDOM(1,52);
				next_card=next_card-1;
				already=0;
			//	return 0;
				}


	}//end main while
	
	for(i=0;i<52;i++)
	{
	last_name[i]=new_name[i];
	last_type[i]=new_type[i];
	}

for(i=0;i<52;i++)
	{
	new_name[i]=0;
	new_type[i]=0;
	}
	
	return 1;
}
///////////////////////////////////////////VALUECARD///////////////////////////////////////////

unsigned char value_card(unsigned char n_no)
{
	unsigned char next_value=0;

	next_value=n_no;

		if(next_value=='A')
			return 1;
		if(next_value=='2')
			return 2;
		if(next_value=='3')
			return 3;
		if(next_value=='4')
			return 4;
		if(next_value=='5')
			return 5;
		if(next_value=='6')
			return 6;
		if(next_value=='7')
			return 7;
		if(next_value=='8')
			return 8;
		if(next_value=='9')
			return 9;
		if(next_value=='0')
			return 10;
		if(next_value=='J')
			return 10;
		if(next_value=='Q')
			return 10;
		if(next_value=='K')
			return 10;
			
			else 
			return 0;
}

/////////////////////////////////////////CHECK THE ADDITION OF POINTSPLAYED/////////////////////////////

unsigned char total_player(void)	// calculate the total value of a hand
	{

		unsigned char value=0,addition=0,i=0;

	
	for (i=0;i<5;i++)
		{
		value=value_card(game_player[i]);
		if (value!=1)
		addition=addition+value;
		}


	for (i=0;i<5;i++)
		{
		value=value_card(game_player[i]);
		if (value==1)
			{
			if ((11+addition)<22)
				addition=addition+11;
			else
				addition=addition+1;
			}
		}

		return addition;
		}
